package com.chanyachting.emall.action.applicationLeter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Applicationleter;
import com.chanyachting.emall.domain.Educationhistory;
import com.chanyachting.emall.domain.Jobhistory;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.form.ApplicationleterForm;
import com.chanyachting.emall.service.ApplicationLeterService;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.ApplicationLeterServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class CreateAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		String selectYear=ru.getParameter("selectYear", "1930");
		String selectMonth=ru.getParameter("selectMonth", "1");
		
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
		
		
		ApplicationleterForm alForm=(ApplicationleterForm)form;
		
		Applicationleter al=new Applicationleter();
		
		BeanUtils.copyProperties(al, alForm);
		
		al.setMemberPicUrl(member.getImgUrl()); //默认设置为会员注册头像
		
		String imgUrl=MyFile.saveFileForm(alForm.getFile(), request);
		
		if(imgUrl!=null && !imgUrl.trim().equals(""))al.setMemberPicUrl(imgUrl);  //设置图片访问地址
		
		al.setCreateDate(DateUtil.getDate()); //设置创建日期
		
		al.setMemberId(member.getId());
		
		al.setStatus("00");   //设置为不公开
		
		al.setInJobDate(selectYear+"-"+selectMonth);  //设置到职日期
		
		//获得所有保存在session中的工作经历
		List jhList=(List)ru.getSessionObject(ApplicationLeterService.JOBHISTORYLISTINSESSINO, new ArrayList());
		
		//获得所有保存在session中的教育背景
		List ehList=(List)ru.getSessionObject(ApplicationLeterService.EDUCATIONHISTORYLISTINSESSINO, new ArrayList());
		
		
		//处理获得的集合
		for(int i=0;i<jhList.size();i++){
			
			Jobhistory jh=(Jobhistory)jhList.get(i);
			
			jh.setApplicationleter(al);
			
			al.addToJobhistorySet(jh);
			
		}
		
		for(int i=0;i<ehList.size();i++){
			
			Educationhistory eh=(Educationhistory)ehList.get(i);
			
			eh.setApplicationleter(al);
			
			al.addToEducationhistorySet(eh);
			
		}
		
		
		ApplicationLeterService alservice=new ApplicationLeterServiceImpl();
		
		boolean temp=alservice.createApplicationleter(al);
		
		if(temp){
			
			//创建成功，清空session信息
			ru.removeSessionObject(ApplicationLeterService.EDUCATIONHISTORYLISTINSESSINO);
			
			ru.removeSessionObject(ApplicationLeterService.JOBHISTORYLISTINSESSINO);
			
			ru.setSuccessMsg();
			
		}else{
			
			ru.setFailMsg();
		}
		
		return mapping.findForward("create");
	}
	
	
	

}
